

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:rapid_kit/rapid_kit.dart';

import '../../../theme.dart';

class PtzPanelWidget extends StatefulWidget {
  final bool panelVisible;
  final String deviceId;

  const PtzPanelWidget({
    super.key,
    required this.deviceId,
    required this.panelVisible,
  });

  @override
  PtzPanelWidgetState createState() => PtzPanelWidgetState();
}

class PtzPanelWidgetState extends State<PtzPanelWidget> {
  final String _tag = '_ptz_panel';
  late Pipe _pipe;

  @override
  void initState() {
    super.initState();
    _pipe = Pipe.create(widget.deviceId);
    _pipe.listen((state) {
      // do nothing currently
    });
    _pipe.establish();
  }

  @override
  void dispose() {
    super.dispose();
    _pipe.abolish();
  }

  @override
  Widget build(BuildContext context) {
    return widget.panelVisible ? _buildPtzPanel() : Container();
  }

  Widget _buildPtzPanel() {
    return Center(
      child: Container(
        color: SampleThemeConfigure.colorBackground,
        child: Center(
          child: Container(
            decoration: BoxDecoration(
              color: Colors.white,
              shape: BoxShape.circle,
            ),
            child: Container(
              width: 200.0,
              height: 200.0,
              decoration: BoxDecoration(
                color: Colors.white,
                shape: BoxShape.circle,
              ),
              child: Stack(
                children: [
                  Positioned(
                    left: 90.0,
                    top: 20.0,
                    child: GestureDetector(
                      onTap: _onArrowUpClick,
                      onLongPressStart: (details) => _onArrowUpLongPressStart(),
                      onLongPressEnd: (details) => _onArrowUpLongPressEnd(),
                      child: Icon(Icons.arrow_upward, color: SampleThemeConfigure.colorPrimary),
                    ),
                  ),
                  Positioned(
                    left: 90.0,
                    bottom: 20.0,
                    child: GestureDetector(
                      onTap: _onArrowDownClick,
                      onLongPressStart: (details) => _onArrowDownLongPressStart(),
                      onLongPressEnd: (details) => _onArrowDownLongPressEnd(),
                      child: Icon(Icons.arrow_downward, color: SampleThemeConfigure.colorPrimary),
                    ),
                  ),
                  Positioned(
                    left: 20.0,
                    top: 90.0,
                    child: GestureDetector(
                      onTap: _onArrowLeftClick,
                      onLongPressStart: (details) => _onArrowLeftLongPressStart(),
                      onLongPressEnd: (details) => _onArrowLeftLongPressEnd(),
                      child: Icon(Icons.arrow_back, color: SampleThemeConfigure.colorPrimary),
                    ),
                  ),
                  Positioned(
                    right: 20.0,
                    top: 90.0,
                    child: GestureDetector(
                      onTap: _onArrowRightClick,
                      onLongPressStart: (details) => _onArrowRightLongPressStart(),
                      onLongPressEnd: (details) => _onArrowRightLongPressEnd(),
                      child: Icon(Icons.arrow_forward, color: SampleThemeConfigure.colorPrimary),
                    ),
                  ),
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }

  void _onArrowUpClick() {
    if (!_pipe.isEstablished) return;
    InstructStandard.ptzMoveByStep(_pipe, Direction.up);
  }

  void _onArrowUpLongPressStart() {
    if (!_pipe.isEstablished) return;
    InstructStandard.ptzMoveByContinuous(_pipe, Direction.up);
  }

  void _onArrowUpLongPressEnd() {
    if (!_pipe.isEstablished) return;
    InstructStandard.ptzStopMoving(_pipe);
  }

  void _onArrowDownClick() {
    if (!_pipe.isEstablished) return;
    InstructStandard.ptzMoveByStep(_pipe, Direction.down);
  }

  void _onArrowDownLongPressStart() {
    if (!_pipe.isEstablished) return;
    InstructStandard.ptzMoveByContinuous(_pipe, Direction.down);
  }

  void _onArrowDownLongPressEnd() {
    if (!_pipe.isEstablished) return;
    InstructStandard.ptzStopMoving(_pipe);
  }

  void _onArrowLeftClick() {
    if (!_pipe.isEstablished) return;
    InstructStandard.ptzMoveByStep(_pipe, Direction.left);
  }

  void _onArrowLeftLongPressStart() {
    if (!_pipe.isEstablished) return;
    InstructStandard.ptzMoveByContinuous(_pipe, Direction.left);
  }

  void _onArrowLeftLongPressEnd() {
    if (!_pipe.isEstablished) return;
    InstructStandard.ptzStopMoving(_pipe);
  }

  void _onArrowRightClick() {
    if (!_pipe.isEstablished) return;
    InstructStandard.ptzMoveByStep(_pipe, Direction.right);
  }

  void _onArrowRightLongPressStart() {
    if (!_pipe.isEstablished) return;
    InstructStandard.ptzMoveByContinuous(_pipe, Direction.right);
  }

  void _onArrowRightLongPressEnd() {
    if (!_pipe.isEstablished) return;
    InstructStandard.ptzStopMoving(_pipe);
  }
}
